﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIZMA._controls;
using BIZModel;
using BL.BIZControls;

namespace BIZMA._controls.Demo
{
    public partial class Edit_Employee : BIZEditControl, IBIZControlDefaultMethods
    {
        #region default
        public void DefaultListLoad()
        {
            throw new NotImplementedException();
        }

        public void DefaultListLoad(Guid Id, string loadByType)
        {
            throw new NotImplementedException();
        }

        public void DefaultEditNew()
        {
            NewEmployee();
        }

        public void DefaultEditNew(Guid Id, string editByType)
        {
            if (editByType.ToUpper() == EditByTypeCompany)
            {
                NewCustomerEmployee(Id);
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        public void DefaultEditLoad(Guid Id)
        {
            LoadEmployee(Id);
        }

        public const string EditByTypeCompany = "COMPANY";
        #endregion

        private Guid CustomerId { get { return ViewState["CustomerId"] != null ? (Guid)ViewState["CustomerId"] : Guid.Empty; } set { ViewState["CustomerId"] = value; } }

        #region This Dialog
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack || !InitLoaded)
            {
                InitLoaded = true;

                //Gör sådant som man gör på pageload här typ fyll "statisk" dropdown med värden mm 

            }
        }
      
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //här gör man alla init inställningar typ lägga till eventhandlers och annat som styr sidan

            if (ContainerControl != null)
            {
                ContainerControl.Title = "Administrera anställd";
                ContainerControl.ButtonsClick += new ButtonsHandler(ContainerControl_ButtonsClick);
                ContainerControl.ShowButtons(true, true, true, true);

                if (ModalContainerControl != null)
                {
                    //Sätt höjden här istället så behöver man inte göra det på alla ställen som poppar detta formulär
                    ModalContainerControl.Width = 500;
                    ModalContainerControl.Height = 470;
                }

                //Valgroup
                valEmail.ValidationGroup = ContainerControl.ValGroupSave;
                valFirstName.ValidationGroup = ContainerControl.ValGroupSave;
                valLastName.ValidationGroup = ContainerControl.ValGroupSave;
                Sel_Companies1.ReqValidationGroup = ContainerControl.ValGroupSave;
            }


         }

        void ContainerControl_ButtonsClick(object sender, ActionType actionType)
        {
            if (actionType == ActionType.Save || actionType == ActionType.SaveClose)
            {
                Save();
            }
            else if (actionType == ActionType.Delete)
            {
                Delete();
            }

        }

        #endregion

        #region methods this dialog
        /// <summary>
        /// Den här funktionen aktiverar inativerar knappar som kräver att formuläret har sparats/laddats och det finns data som 
        /// knappen kan använda ex Man kan inte öppna en planering för ett projekt om inte projektet har skapats/laddats först
        /// </summary>
        private void ToggleButtons()
        {

        }

        /// <summary>
        /// Metod för att tömma sida och aktivera läge för att skapa nytt
        /// </summary>
        public void NewEmployee()
        {
            //CurrentProjectId = Guid.Empty;
            CustomerId = Guid.Empty;
            LoadEmployee(Guid.Empty);
            
        }

        public void NewCustomerEmployee(Guid customerId)
        {
            CustomerId = customerId;
            LoadEmployee(Guid.Empty);
        }


        /// <summary>
        /// ladda formuläret med existerande data för att ex uppdatera
        /// </summary>
        /// <param name="someVal"></param>
        public void LoadEmployee(Guid employeeId)
        {
            InitLoaded = true;
            CurrentId = employeeId;


            if (employeeId != Guid.Empty)
            {
                BIZMAEntities ent = BaseBL.GetEnt();
                BIZModel.DemoEmployee emp = ent.DemoEmployees.FirstOrDefault(q => q.EmployeeId == employeeId);

                txtFirstName.Text = emp.FirstName;
                txtLastName.Text = emp.LastName;
                Sel_Companies1.SelectedId = emp.CompanyId;
                txtEmail.Text = emp.Email;
                txtPhone.Text = emp.Phone;
                txtStartDate.Text = emp.StartDate.HasValue?  emp.StartDate.ToString() : "";
                txtEndDate.Text = emp.EndDate.HasValue ? emp.EndDate.ToString() : "";
            }
            else
            {
                txtFirstName.Text ="";
                txtLastName.Text = "";
                Sel_Companies1.SelectedId = CustomerId; //fixa sedan så att man kan välja i förväg vilken kund det ska vara
                txtEmail.Text = "";
                txtPhone.Text = "";
                txtStartDate.Text = "";
                txtEndDate.Text = "";
            }

            ToggleButtons();
        }


        private void Save()
        {
            //SAVE
            BIZMAEntities ent = BaseBL.GetEnt();
            BIZModel.DemoEmployee emp = null;
           

            //ent.SaveChanges();
            if (CurrentId == Guid.Empty)
            {
                CurrentId = Guid.NewGuid();
                emp = DemoEmployee.CreateDemoEmployee(CurrentId, 0, txtFirstName.Text, txtLastName.Text, Sel_Companies1.SelectedId);
                emp.FirstName = txtFirstName.Text;
                emp.LastName = txtLastName.Text;
                emp.CompanyId = Sel_Companies1.SelectedId;
                emp.Email = txtEmail.Text;
                emp.Phone = txtPhone.Text;
                emp.StartDate = txtStartDate.Text == "" ? (DateTime?)null : DateTime.Parse(txtStartDate.Text);
                emp.EndDate = txtEndDate.Text == "" ? (DateTime?)null : DateTime.Parse(txtEndDate.Text);
                ent.AddToDemoEmployees(emp);
               
               
            }
            else
            {
                emp = ent.DemoEmployees.FirstOrDefault(q => q.EmployeeId == CurrentId);
                emp.FirstName = txtFirstName.Text;
                emp.LastName = txtLastName.Text;
                emp.CompanyId = Sel_Companies1.SelectedId;
                emp.Email = txtEmail.Text;
                emp.Phone = txtPhone.Text;
                emp.StartDate = txtStartDate.Text == "" ? (DateTime?)null : DateTime.Parse(txtStartDate.Text);
                emp.EndDate = txtEndDate.Text == "" ? (DateTime?)null : DateTime.Parse(txtEndDate.Text);
            }


            ent.SaveChanges();

            //Ändra aktivering av knappar efter sparning
            LoadEmployee(CurrentId);
        }

        private void Delete()
        {
            BIZMAEntities ent = BaseBL.GetEnt();
            BIZModel.DemoEmployee emp = ent.DemoEmployees.FirstOrDefault(q => q.EmployeeId == CurrentId);
            ent.DeleteObject(emp);
            ent.SaveChanges();
            
        }
        #endregion







    }
}